// pages/index/index.js
Page({
    data: {
      categories: [//用于展示分类标签。
        { id: 1, name: '感冒发烧' },
        { id: 2, name: '眼耳口鼻' },
        { id: 3, name: '营养保健' },
        { id: 4, name: '儿童专区' },
        { id: 5, name: '肠胃用药' },
        { id: 6, name: '美容养颜' }
      ],
      currentCategoryId: 1,
      products: [],//存储当前分类下的商品数据
      keyword: ''
    },
    onLoad() {
      this.switchCategory({ currentTarget: { dataset: { id: 1 } } });
    },
    switchCategory(e) {//该函数接收一个事件对象 e，从事件对象中获取点击的分类 ID。
      const categoryId = e.currentTarget.dataset.id;
      this.setData({
        currentCategoryId: categoryId,
        products: this.getProductsByCategory(categoryId)
      });
    },
    getProductsByCategory(categoryId) {
      const productData = {
        1: [
          { id: 1, name: 'test1', price: 20, imageUrl: 'https://image.jianke.com/mall/product/202106/3b27b6b1746d41ec963c25a822d0c91c.jpg', efficacy: '解热镇痛，用于感冒引起的头痛、发热、鼻塞、流涕、咽痛等。' },
          { id: 2, name: 'test2', price: 12, imageUrl: 'https://img.yaopinnet.com/img500/202304/ttrp7m1682505827.jpg', efficacy: '疏风散寒，解表清热。用于风寒感冒，头痛发热，恶寒身痛，鼻流清涕，咳嗽咽干。' }
        ],
        2: [
          { id: 3, name: 'test3', price: 13, imageUrl: 'https://img.alicdn.com/bao/uploaded/i2/3079557865/O1CN01VPG9Iv27yDvAx5S7H_!!0-item_pic.jpg', efficacy: '鼻塞流涕，急慢性鼻炎，过敏性鼻炎' },
          { id: 4, name: 'test4', price: 11, imageUrl: 'https://m.360buyimg.com/mobilecms/s750x750_jfs/t1/114892/26/2955/311564/5ea54984E9c7a905e/b9538815efaaf11d.jpg!q80.dpg', efficacy: '对于口腔溃疡、牙龈肿痛等口腔黏膜损伤，可加速受损组织修复，缩短病程。' }
        ],
        3: [
          { id: 5, name: '商品 3-1', price: 0, imageUrl: 'https://img00.miaoshoucdn.com/product/watermark/2/33/2339bd29f5fb7cbde0517237d37dab25.jpg', efficacy: '用于治疗维生素A缺乏症，如夜盲症、干眼病、角膜软化症和皮肤粗糙等' },
          { id:6, name: '商品 3-2', price: 0, imageUrl: 'https://image.jianke.com/mall/product/202403/f9afc318cc2b488daaa7ec601eaadd1f.jpg', efficacy: '维持和改善上皮组织，如眼睛的上皮组织、消化道黏膜组织的健康' }
        ],
        4: [
          { id: 7, name: '商品 4-1', price: 0, imageUrl: 'https://tse4-mm.cn.bing.net/th/id/OIP-C.MZJcYU9GJ2M0HuMsk8RSQQHaHk?cb=iwc1&rs=1&pid=ImgDetMain', efficacy: '暂无功效信息' },
          { id: 8, name: '商品 4-2', price: 0, imageUrl: 'https://img.alicdn.com/bao/uploaded/O1CN016K9OFP1FWkpsb9Xbr_!!6000000000495-0-yinhe.jpg', efficacy: '暂无功效信息' }
        ],
        5: [
          { id: 9, name: '商品 5-1', price: 0, imageUrl: 'https://image.jianke.com/upload/prodimage/201603wm/2016314162632378.jpg', efficacy: '暂无功效信息' },
          { id: 10, name: '商品 5-2', price: 0, imageUrl: 'https://image.jianke.com/upload/prodimage/201604wm/2016420165456455.jpg', efficacy: '暂无功效信息' }
        ],
        6: [
          { id: 11, name: '商品 6-1', price: 0, imageUrl: 'https://ts1.tc.mm.bing.net/th/id/R-C.282b4e626d6bf64ab21efce5e60ba641?rik=DHFEBNDoGAxCJQ&riu=http%3a%2f%2fupload.qianlong.com%2f2021%2f0804%2f1628068305374.png&ehk=VoRCxDNnFfbRysYq5L8nWdAqC9w4JaXWa3%2fctdwiefE%3d&risl=&pid=ImgRaw&r=0', efficacy: '暂无功效信息' },
          { id: 12, name: '商品 6-2', price: 0, imageUrl: 'https://cbu01.alicdn.com/img/ibank/O1CN01p1IL4T1mgGOJfl1L3_!!3856244983-0-cib.jpg', efficacy: '暂无功效信息' }
        ]
      };
      return productData[categoryId] || [];
    },
    onInput(e) {
      this.setData({
        keyword: e.detail.value
      });
    },
    onSearch() {
      const keyword = this.data.keyword;
      const currentCategoryId = this.data.currentCategoryId;
      this.setData({
        products: this.getProductsByCategory(currentCategoryId).filter(item =>
          item.name.includes(keyword)
        )
      });
    },
    toDetail(e) {
      const id = e.currentTarget.dataset.id;
      const product = this.getProductsByCategory(this.data.currentCategoryId).find(item => item.id === id);
      wx.navigateTo({//使用 wx.navigateTo 方法跳转到商品详情页
        url: `../detail/detail?id=${id}&efficacy=${encodeURIComponent(product.efficacy)}`
      });
    }
  });